package com.musigma.util;


import java.util.StringTokenizer;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;


/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
/**
 *
 * @author krishna
 */
public class RadTool {

    protected static final Log log = LogFactory.getLog(RadTool.class);
    static ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(new String[]{"classpath:ApplicationContext.xml"});
    static BeanFactory factory = (BeanFactory) appContext;
    static private DataSource dataSource = (DataSource) factory.getBean("oracleDataSource");
    static private JdbcTemplate jdbcTemp = new JdbcTemplate(dataSource);

    public static void main(String args[]) throws Exception {
        System.out.println("Hi Krishna");
        String selectSMT = "select * from smtb_user";
        System.out.println(selectSMT);
        org.springframework.jdbc.support.rowset.SqlRowSetMetaData a = jdbcTemp.queryForRowSet(selectSMT).getMetaData();
        for (int columnCount = 1; columnCount <= a.getColumnCount(); columnCount++) {
            System.out.println("private " + getJavaTypeForOracleType(a.getColumnTypeName(columnCount)) + " " + getInitCapTokanizers2(a.getColumnName(columnCount)) + " ; ");
        }
        System.out.println("--------------------------------------------------------------------");
        for (int columnCount = 1; columnCount <= a.getColumnCount(); columnCount++) {
            System.out.println("setterMapObject.set" + getInitCapTokanizers1(a.getColumnName(columnCount)) + "(resultSetOBJ."+getJavaRSType(a.getColumnTypeName(columnCount))+"(\""+a.getColumnName(columnCount)+"\"));");
        }

    }

    static String getJavaTypeForOracleType(String oracleType) {
        if ("VARCHAR2".equals(oracleType)) {
            return "String";
        } else if ("CHAR".equals(oracleType)) {
            return "String";
        } else if ("RAW".equals(oracleType)) {
            return "String";
        } else if ("TIMESTAMP".equals(oracleType)) {
            return "Date";
        } else {
            return oracleType;
        }
    }
    
    static String getJavaRSType(String oracleType) {
        if ("VARCHAR2".equals(oracleType)) {
            return "getString";
        } else if ("CHAR".equals(oracleType)) {
            return "getString";
        } else if ("RAW".equals(oracleType)) {
            return "getString";
        } else if ("TIMESTAMP".equals(oracleType)) {
            return "getDate";
        } else {
            return oracleType;
        }
    }

    static String getInitCapTokanizers1(String columnName) {
        String filedName = "";
        StringTokenizer stringTokenizer = new StringTokenizer(columnName.toLowerCase(), "_");
        while (stringTokenizer.hasMoreTokens()) {
            filedName = filedName + initCap(stringTokenizer.nextToken());
        }
        return filedName;
    }

    static String getInitCapTokanizers2(String columnName) {
        String filedName = "";
        StringTokenizer stringTokenizer = new StringTokenizer(columnName.toLowerCase(), "_");
        int skipCount = 1;
        while (stringTokenizer.hasMoreTokens()) {
            //log.info(initCap(stringTokenizer.nextToken()));
            if (skipCount == 1) {
                filedName = filedName + (stringTokenizer.nextToken());
            } else {
                filedName = filedName + initCap(stringTokenizer.nextToken());
            }
            skipCount++;
        }
        return filedName;
    }

    static String initCap(String s) {
        return (s != null)
                ? s.substring(0, 1).toUpperCase()
                + s.substring(1, s.length()).toLowerCase()
                : null;
    }
}
